package com.jk1123.dotdog.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Bootstrap {
    private int threadNum=20;
    public ExecutorService initThreadPools() {
        ExecutorService pool = Executors.newFixedThreadPool(threadNum);
        return pool;
    }

    public ServerSocket bind(int port){
        try {
            return new ServerSocket(port);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public void start(ExecutorService workerPool, ServerSocket serverSocket, ServletContainer servletContainer) {
        while (true){
            try {
                Socket socket = serverSocket.accept();
                workerPool.submit(new SocketProcessor(servletContainer,socket));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
